ConfSale = new Object();

ConfSale.getFieldSet1 = function() {

	var finalFieldSet = Ext.create('Ext.form.FieldSet', {
		title : 'Basic',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Fund",
				inputValue : 'confirmSaleFundCmb',
				id : 'confSaleFundCmb',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : "Value Date",
				inputValue : 'confirmSaleValDate',
				id : 'confSaleValDate',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'checkboxfield',
			boxLabel : "Instrument Type",
			inputValue : 'confirmSaleInsTypeCmb',
			id : 'confSaleInsTypeCmb',
			flex : 1,
			padding : '0 10 0 10',
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Instrument Code",
				inputValue : 'confirmSaleInsCodeCmb',
				id : 'confSaleInsCodeCmb',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : "Buyer",
				inputValue : 'confirmSaleIssuerCmb',
				id : 'confSaleIssuerCmb',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'checkboxfield',
			boxLabel : "Secondary Market",
			inputValue : 'confirmSaleSecMarket',
			id : 'confSaleSecMarket',
			flex : 1,
			padding : '0 10 0 10',
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Buyer's Name",
				inputValue : 'confirmSaleBuyerName',
				id : 'confSaleBuyerName',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : "Bank Account",
				inputValue : 'confirmSaleBuyerBankAc',
				id : 'confSaleBuyerBankAc',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Delevary Method',
				inputValue : 'confirmSaleDelevaryMethod',
				id : 'confirmSaleDelevaryMethod',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Dealer',
				inputValue : 'confirmSaleDealer',
				id : 'confirmSaleDealer',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'checkboxfield',
			boxLabel : 'Settlement Method',
			inputValue : 'confirmSaleSettlementMethod',
			id : 'confirmSaleSettlementMethod',
			flex : 1,
			padding : '0 10 0 10',
		} ]
	});
	return finalFieldSet;
};

ConfSale.getFieldSet3 = function() {

	var finalFieldSet = Ext.create('Ext.form.FieldSet', {
		title : 'Sale Information',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'vbox',
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				defaultType : 'checkboxfield',
				items : [ {
					boxLabel : "Selling Value",
					inputValue : 'confirmSaleDealVal',
					id : 'confPurchDealVal',
					flex : 1,
					padding : '0 10 0 10',
				}, {
					boxLabel : "Selling Rate",
					inputValue : 'confirmSaleSallingRate',
					id : 'confSaleSallingRate',
					flex : 1,
					padding : '0 10 0 10',
				}, ]
			}, {
				xtype : 'checkboxfield',
				boxLabel : "Placement Fee",
				inputValue : 'confirmSalePlacementFee',
				id : 'confSalePlacementFee',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				xtype : 'checkboxfield',
				boxLabel : "Partial Sale",
				inputValue : 'confirmSalePartialSale',
				id : 'confSalePartialSale',
				flex : 1,
				padding : '0 10 0 10',
				listeners : {
					change : function(field, newVal) {
						if (!newVal) {
							Ext.ComponentQuery.query('#confSaleSellingAmount')[0].setValue(false);
						}
					}
				}
			}, {
				xtype : 'checkboxfield',
				boxLabel : "Selling Amount",
				inputValue : 'confirmSaleSellingAmount',
				id : 'confSaleSellingAmount',
				flex : 1,
				padding : '0 10 0 10',
				listeners : {
					change : function(field, newVal) {
						if (newVal) {
							Ext.ComponentQuery.query('#confSalePartialSale')[0].setValue(true);
						}
					}
				}
			} ]
		} ]
	});
	return finalFieldSet;
};

ConfSale.getCenterPanel = function() {
	var filedset1 = ConfSale.getFieldSet1();

	var filedset3 = ConfSale.getFieldSet3();

	var formPanel = Ext.create('Ext.form.FormPanel', {
		bodyPadding : '20',
		id : 'confSaleFormPanel',
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 175,
			anchor : '68%',
		},
		fieldDefaults : {
			labelAlign : 'left',
			labelWidth : 150,
		},
		title : 'Record SALE',
		items : [ filedset1, filedset3 ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				// Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
			}
		}, {
			text : 'Save',
			itemId : 'saveButton',
			handler : function() {
				var form = this.up('form').getForm();
				var valueSet = '';
				var localFieldSet = '';

				if (Ext.ComponentQuery.query('#confSaleFundCmb')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleFundCmb')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleFundCmb;';
				}

				if (Ext.ComponentQuery.query('#confSaleValDate')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleValDate')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleValDate;';
				}

				if (Ext.ComponentQuery.query('#confSaleInsTypeCmb')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleInsTypeCmb')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleInsTypeCmb;';
				}

				if (Ext.ComponentQuery.query('#confSaleInsCodeCmb')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleInsCodeCmb')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleInsCodeCmb;';
				}

				if (Ext.ComponentQuery.query('#confSaleIssuerCmb')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleIssuerCmb')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleIssuerCmb;';
				}

				if (Ext.ComponentQuery.query('#confSaleSecMarket')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleSecMarket')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleSecMarket;';
				}

				if (Ext.ComponentQuery.query('#confSaleBuyerName')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleBuyerName')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleBuyerName;';
				}

				if (Ext.ComponentQuery.query('#confSaleBuyerBankAc')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleBuyerBankAc')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleBuyerBankAc;';
				}

				if (Ext.ComponentQuery.query('#confPurchDealVal')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchDealVal')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchDealVal;';
				}

				if (Ext.ComponentQuery.query('#confSaleSallingRate')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleSallingRate')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleSallingRate;';
				}

				if (Ext.ComponentQuery.query('#confSalePlacementFee')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSalePlacementFee')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSalePlacementFee;';
				}

				if (Ext.ComponentQuery.query('#confSalePartialSale')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSalePartialSale')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSalePartialSale;';
				}

				if (Ext.ComponentQuery.query('#confSaleSellingAmount')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confSaleSellingAmount')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confSaleSellingAmount;';
				}

				if (Ext.ComponentQuery.query('#confirmSaleDelevaryMethod')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confirmSaleDelevaryMethod')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confirmSaleDelevaryMethod;';
				}

				if (Ext.ComponentQuery.query('#confirmSaleDealer')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confirmSaleDealer')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confirmSaleDealer;';
				}

				if (Ext.ComponentQuery.query('#confirmSaleSettlementMethod')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confirmSaleSettlementMethod')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confirmSaleSettlementMethod;';
				}

				form.url = 'newConfField.action';
				formPanel.setLoading('Saving...');
				form.submit({
					params : {
						'screen' : 'RecordSale',
						'values' : valueSet,
						'localFields' : localFieldSet,
						'user' : 'SaleConfirmer',
					},
					success : function(form, action) {
						formPanel.setLoading(false);
						Ext.Msg.alert('Success', 'Fields Saved!.');
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed!', 'There is some error returned from the server.');
						formPanel.setLoading(false);
					}
				});
			}
		} ]
	});

	Ext.Ajax.request({
		url : 'confLocalFields.action',
		params : {
			screen : 'RecordSale',
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			confirmFields = obj.values;

			var confFieldsArrray = confirmFields.split(";");
			var i;
			var confFieldLocal;
			var confFieldQuery;
			for (i = 0; i < confFieldsArrray.length - 1; i++) {
				confFieldQuery = '#' + confFieldsArrray[i];
				confFieldLocal = Ext.ComponentQuery.query(confFieldQuery)[0];
				confFieldLocal.setValue(true);
			}
			// formPanel.setLoading(false);
		},
		failure : function(form, action) {
			// formPanel.setLoading(false);
			Ext.Msg.alert('Error!', 'Failed to load Confirmation Fields!');
		}
	});

	return formPanel;
};
